package pit.com.ordermobilesystem;

import pit.com.ordermobilesystem.adapter.OrderItemAdapter;
import pit.com.ordermobilesystem.model.Item;
import pit.com.ordermobilesystem.model.OrderItem;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class SelectedFragment extends Fragment {
	private ListView lv_danhsach = null;
	private TextView tv_tongcong;
	private ImageButton imbt_delete;
	private ImageView imv_trove;
	private ImageView imv_thanhtoan;

	private OrderItemAdapter adapter = null;
	public static int tien = 0;
	private Item item;
	

	public Item getItem() {
		return item;
	}
	public void setItem(Item itemnew) {
		item = itemnew;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.selectedfragment, container,
				false);
		tv_tongcong = (TextView) rootView.findViewById(R.id.tv_tongcong);
		imbt_delete = (ImageButton) rootView.findViewById(R.id.imbt_delete);
		imv_trove = (ImageView) rootView.findViewById(R.id.iv_trove);
		imv_thanhtoan = (ImageView) rootView.findViewById(R.id.iv_thanhtoan);

		// cai nay chi mo phong da co OrderItem roi

		// khi da co OrderItemList tinh tien trong OrderItemList dang co

		setView(rootView);
		return rootView;
	}

	public void setView(View v) {

		tien = 0;
		for (int j = 0; j < DetailsFragment.ListItem.size(); j++) {
			OrderItem newitem = new OrderItem();
			newitem = DetailsFragment.ListItem.get(j);
			int t = (int) newitem.getPrice();
			tien = tien + newitem.getNumberItem()*t;
		}
		lv_danhsach = (ListView) v.findViewById(R.id.lv_danhsach);
		adapter = new OrderItemAdapter(getActivity(), R.layout.seclectiem,
				DetailsFragment.ListItem);
		lv_danhsach.setAdapter(adapter);
		tv_tongcong.setText(String.valueOf(tien)+".000 đ");

		imbt_delete.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sulyxoa();
			}
		});

		imv_trove.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

					// coi lai back nen quay lai cai ExpanList
					DetailsFragment Details = new DetailsFragment();
					Details.setItem(item);
					FragmentTransaction transaction = getFragmentManager().beginTransaction();
					transaction.replace(R.id.details_frag, Details);
					transaction.addToBackStack(null);
					transaction.commit();

			}
		});
		imv_thanhtoan.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
					PaymentFragment Payment = new PaymentFragment();
					Payment.setItem(item);
					FragmentTransaction transaction = getFragmentManager()
							.beginTransaction();
					transaction.replace(R.id.details_frag, Payment);
					transaction.addToBackStack(null);
					transaction.commit();

			}
		});

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	// su ly hanh dong muon xoa 1 cai OrderItem khoi danh sach da chon
	public void sulyxoa() {
		for (int i = lv_danhsach.getChildCount() - 1; i >= 0; i--) {

			View v = lv_danhsach.getChildAt(i);
			CheckBox chk = (CheckBox) v.findViewById(R.id.cb_click);
			// Nếu nó Checked thì xóa ra khỏi
			if (chk.isChecked()) {
				// xóa phần tử thứ i ra khỏi danh sách
				DetailsFragment.ListItem.remove(i);
			}

		}
		// sau khi xoa , phai tinh lai so tien cua Don hang
		tien = 0;
		for (int j = 0; j < DetailsFragment.ListItem.size(); j++) {
			OrderItem newitem = new OrderItem();
			newitem = DetailsFragment.ListItem.get(j);
			int t =(int) newitem.getPrice();
			tien = tien + newitem.getNumberItem()*t;
		}
		// Sau khi xóa xong thì gọi update giao diện
		adapter.notifyDataSetChanged();
		lv_danhsach.setAdapter(adapter);
		tv_tongcong.setText(String.valueOf(tien) + ".000đ");
	}

}
